package erQiao.yfl.app.controller;

import erQiao.yfl.app.common.Result;
import erQiao.yfl.app.controller.Vo.TopicVo;
import erQiao.yfl.app.dao.entity.Topic;
import erQiao.yfl.app.service.CommentService;
import erQiao.yfl.app.service.TopicService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 *话题
 */
@Controller
@RequestMapping("/topic")
public class TopicController {
    @Resource
    private TopicService topicService;

    @Resource
    private CommentService commentService;

    /**
     * 新增
     * @param topicVo
     */
    @RequestMapping("/insertCircle")
    public void insertCircle(@RequestBody TopicVo topicVo){
        Topic topic = new Topic();
        BeanUtils.copyProperties(topicVo,topic);
        topicService.insert(topic);
    }

    /**
     * 更新
     * @param topicVO
     */
    @RequestMapping("/updateByPrimaryKey")
    public void updateByPrimaryKey(@RequestBody TopicVo topicVO){
        Topic topic = new Topic();
        BeanUtils.copyProperties(topicVO,topic);
        topicService.updateByPrimaryKey(topic);
    }

    @RequestMapping("/delTopicById")
    public void delTopicById(HttpServletRequest request){
        int id = Integer.valueOf(request.getParameter("id"));
        topicService.deleteByPrimaryKey(id);
    }

    /**
     * 点赞
     * @param
     */
    @RequestMapping("/updteUpvote")
    public void commentCircle(HttpServletRequest request){
        int id = Integer.valueOf(request.getParameter("id"));
        topicService.updteUpvote(id);
    }


    /**
     * 查登陆人的话题
     * @param request
     * @return
     */
    @RequestMapping("/selectMyTopicByUserId")
    public Result selectMyCircleByUserId(HttpServletRequest request){
        List<TopicVo> topicVo = new ArrayList<TopicVo>();
        int id = Integer.valueOf(request.getParameter("id"));
        List<Topic> topic = topicService.selectByUserId(id);
        BeanUtils.copyProperties(topicVo,topic);
        if (topic.size()!=0) {
            for(int i = 0;i<topicVo.size();i++){
          /*      int count = commentService.selectComentCountByCid(topic.get(i).getId(),2);
                topicVo.get(i).setCount(count);*/
            }
        }
        return Result.wrapSuccessResult(topicVo);
    }


}
